﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Test_WebDriver.Utilities;
using OpenQA.Selenium;
using OpenQA.Selenium.Interactions;
using System.Collections.ObjectModel;

namespace Test_WebDriver
{
	/// <summary>
	///		<para>Summary: ...</para>
	///		<para>Author: Heller (Hellersong@outlook.com)</para>
	///		<para>Creation: 7/11/2015 7:19:41 PM</para>
	/// </summary>
	[TestClass]
	public class TestWebUtility
	{
		[TestMethod]
		public void test_initialize()
		{
			try
			{
				// Driver is null when not initialize it
				Console.WriteLine(WebUtility.Driver.GetType().ToString());
			}
			catch (Exception)
			{
				throw;
			}
			finally
			{
				WebUtility.DestroyDriver();
			}
		}

		[TestMethod]
		public void test_create()
		{
			try
			{
				WebUtility.CreateDriver();
				WebUtility.OpenUrl("www.baidu.com");
				Console.WriteLine(WebUtility.Wait.GetType().ToString());
				Console.WriteLine(WebUtility.Driver.Title);
			}
			catch (Exception)
			{
				throw;
			}
			finally
			{
				WebUtility.DestroyDriver();
			}
		}

		[TestMethod]
		public void test_demo_for_baidu_site()
		{
			try
			{
				WebUtility.CreateDriver(BrowserType.IE);
				WebUtility.OpenUrl("https://www.baidu.com/");
				IWebElement txtBox = WebUtility.GetElementById("kw");
				txtBox.Click();
				WebUtility.SetValue("Ss", txtBox);
				WebUtility.Actions.SendKeys(Keys.Escape).Perform();
				WebUtility.PauseSecond(1);
				IWebElement searchBtn = WebUtility.GetElementById("su");
				searchBtn.Click();
				WebUtility.PauseSecond(5);
			}
			catch (Exception)
			{
				throw;
			}
			finally
			{
				WebUtility.DestroyDriver();
			}
		}

		[TestMethod]
		public void test_element_not_fount_exception()
		{
			try
			{
				WebUtility.CreateDriver();

				WebUtility.OpenUrl("https://www.baidu.com/");
				// It will throw OpenQA.Selenium.NoSuchElementException
				IWebElement txtBox = WebUtility.GetElementById("ss");
				txtBox.Click();
				WebUtility.PauseSecond();
			}
			catch (Exception)
			{
				throw;
			}
			finally
			{
				WebUtility.DestroyDriver();
			}
		}

		[TestMethod]
		public void test_element_found_timeout()
		{
			// I don't know how to simulate this process.
		}


		[TestMethod]
		public void test_window_handles()
		{
			try
			{
				WebUtility.CreateDriver();

				WebUtility.OpenUrl("https://www.baidu.com/");
				IWebElement hao123 = WebUtility.GetElementByCssSelector(@"a[href='http://www.hao123.com']");
				IWebElement news = WebUtility.GetElementByCssSelector(@"a[href='http://news.baidu.com']");
				IWebElement tieba = WebUtility.GetElementByCssSelector(@"a[href='http://tieba.baidu.com']");

				hao123.SendKeys( Keys.Return);
				
				news.SendKeys(Keys.Control + Keys.Return);
				tieba.SendKeys(Keys.Control + Keys.Return);
				WebUtility.PauseSecond(3);
				int i = 0;
				foreach (String s in WebUtility.Driver.WindowHandles)
				{
					Console.Write("Window Index[" + i + "]: ");
					Console.Write(s + ", ");
					WebUtility.Driver.SwitchTo().Window(s);
					WebUtility.PauseSecond();
					Console.WriteLine("Title: " + WebUtility.Driver.Title);
					i++;
				}
			}
			catch (Exception)
			{
				throw;
			}
			finally
			{
				WebUtility.DestroyDriver();
			}
		}

		[TestMethod]
		public void test_window_handles_recursive()
		{
			try
			{
				WebUtility.CreateDriver();

				WebUtility.OpenUrl("https://www.baidu.com/");
				IWebElement hao123 = WebUtility.GetElementByCssSelector(@"a[href='http://www.hao123.com']");
				hao123.SendKeys(Keys.Control + Keys.Return);
				WebUtility.PauseSecond(3);
				int i = 0;
				foreach (String s in WebUtility.Driver.WindowHandles)
				{
					Console.Write("Window Index[" + i + "]: ");
					Console.Write(s + ", ");
					WebUtility.Driver.SwitchTo().Window(s);
					WebUtility.PauseSecond();
					Console.WriteLine("Title: " + WebUtility.Driver.Title);
					i++;
				}

				Console.WriteLine();
				// Currently my driver is point to the "Hao123" window
				// In this window to open a new window, to test all web window handles
				IWebElement iqiyi = WebUtility.GetElementByCssSelector(@"a[href='http://www.iqiyi.com/']");
				iqiyi.SendKeys(Keys.Control + Keys.Return);
				WebUtility.PauseSecond(3);
				i = 0;
				foreach (String s in WebUtility.Driver.WindowHandles)
				{
					Console.Write("Window Index[" + i + "]: ");
					Console.Write(s + ", ");
					WebUtility.Driver.SwitchTo().Window(s);
					WebUtility.PauseSecond();
					Console.WriteLine("Title: " + WebUtility.Driver.Title);
					i++;
				}
			}
			catch (Exception)
			{
				throw;
			}
			finally
			{
				WebUtility.DestroyDriver();
			}
		}

		[TestMethod]
		public void test_window_switch_pop_window()
		{
			try
			{
				WebUtility.CreateDriver();

				WebUtility.OpenUrl("https://www.baidu.com/");
				IWebElement hao123 = WebUtility.GetElementByCssSelector(@"a[href='http://www.hao123.com']");
				hao123.SendKeys(Keys.Control + Keys.Return);
				WebUtility.PauseSecond(3);
				WebUtility.SwitchToPopupWindow();
				WebUtility.Driver.Manage().Window.Maximize();
				WebUtility.PauseSecond();
				WebUtility.Driver.Close();
				WebUtility.PauseSecond();
			}
			catch (Exception)
			{
				throw;
			}
			finally
			{
				WebUtility.DestroyDriver();
			}
		}


		[TestMethod]
		public void test_keyboard_operaction()
		{
			WebUtility.CreateDriver();

			WebUtility.OpenUrl("https://www.baidu.com/");
			IWebElement hao123Link = WebUtility.GetElementByCssSelector("a[href='http://www.hao123.com']");

			Actions myAction = new Actions(WebUtility.Driver);
			myAction.SendKeys(Keys.Control + "t");
			myAction.Build().Perform();
			WebUtility.PauseSecond();

			Console.WriteLine(WebUtility.Driver.WindowHandles.Count);

			//*// Always open link in new window
			hao123Link.SendKeys(Keys.Control + Keys.Return);
			WebUtility.PauseSecond();

			WebUtility.DestroyDriver();
		}

	}
}
